package com.woniu.user.controller;

import com.woniu.common.entity.ResponseResult;
import com.woniu.common.entity.User;
import com.woniu.user.entity.Message;
import com.woniu.user.service.ApplyService;
import com.woniu.user.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author author
 * @create 2021/5/20
 */
@RequestMapping("/message")
@RestController
public class MessageController {
    @Autowired
    ApplyService applyService;

    @Autowired
    MessageService messageService;

    @GetMapping("/all")
    public ResponseResult<List<Message>> selectAllMessage() {
        return new ResponseResult<List<Message>>(200, null, "查询成功", messageService.selectAllMessage(), null);
    }

    @RequestMapping("/add")
    public ResponseResult<String> addMessage(@RequestBody Message message) {
        if (messageService.addMessage(message)) {
            return new ResponseResult<>(200, null, "添加成功", null, null);
        }
        return new ResponseResult<>(500, null, "添加失败", null, null);
    }

    //个人消息查询
    @RequestMapping("/selectMesage")
    public List<com.woniu.common.entity.Message> selectMesage(String account){
        int uid=applyService.selectUser(account).getId();
        List<com.woniu.common.entity.Message> list=applyService.selectMessage(uid);
        return list;
    }
    //消息通知接口
    @RequestMapping("/insertMessage")
    public ResponseResult<String> insertMessage(@RequestBody com.woniu.common.entity.Message message){
        int setStatus=0;
        message.setStatus(setStatus);
        message.setMessage_time(new Date());
        applyService.insertMessage(message);

        return new ResponseResult<String>(200,null,"成功",null,null);
    }
    //
    @RequestMapping("/user/selectUser")
    public User selectUser(@RequestBody String name){
        return messageService.selectUser(name);
    };

}
